//
//  XKLPictureView.m
//  GuiZiShouDuanZi
//
//  Created by xmg on 16/5/5.
//  Copyright © 2016年 xiao_kl. All rights reserved.
//

#import "XKLPictureView.h"
#import "XKLEessenceItem.h"
#import <UIImageView+WebCache.h>
#import "XKLProgress.h"
#import "XKLShowPictureViewController.h"

@interface XKLPictureView()
@property (weak, nonatomic) IBOutlet UIImageView *gifImageView;
@property (weak, nonatomic) IBOutlet UIImageView *pictureImageView;
@property (weak, nonatomic) IBOutlet UIButton *ClickBut;
@property (weak, nonatomic) IBOutlet XKLProgress *progressView;

@end

@implementation XKLPictureView

+(instancetype)picture{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
}

-(void)awakeFromNib{
    //取消系统布局
    self.autoresizingMask = UIViewAutoresizingNone;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGes:)];
    [self.pictureImageView addGestureRecognizer:tap];
}
//手势
-(void)tapGes:(UITapGestureRecognizer *)tap{
    
    XKLShowPictureViewController *showPicture = [[XKLShowPictureViewController alloc] init];
    showPicture.item = _item;
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:showPicture animated:YES completion:nil];
    
}
-(void)setItem:(XKLEessenceItem *)item{
    _item = item;
    //因为循环利用,使用要清零
    [self.progressView setProgress:item.imageProgress animated:NO];
    
    self.pictureImageView.contentMode = UIViewContentModeScaleAspectFit;
    if (item.is_gif) {
        self.gifImageView.hidden = NO;
        self.ClickBut.hidden = YES;
        [self.pictureImageView sd_setImageWithURL:[NSURL URLWithString:item.image1] placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            
            item.imageProgress = (CGFloat)receivedSize / expectedSize;
            [self.progressView setProgress:item.imageProgress animated:NO];
            
        } completed:nil];
    }else if (item.isBigPicture){
        self.gifImageView.hidden = YES;
        self.ClickBut.hidden = NO;
        self.pictureImageView.contentMode = UIViewContentModeTop;
        [self.pictureImageView sd_setImageWithURL:[NSURL URLWithString:item.image1] placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            
            item.imageProgress = (CGFloat)receivedSize / expectedSize;
            [self.progressView setProgress:item.imageProgress animated:NO];
            
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            //开启上下文
            UIGraphicsBeginImageContextWithOptions(item.bigPictureSize, NO, [UIScreen mainScreen].scale);
            [image drawInRect:CGRectMake(0, 0, item.bigPictureSize.width, item.bigPictureSize.height)];
            UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
            
            //关闭上下文
            UIGraphicsEndImageContext();
            self.pictureImageView.image = newImage;
            //替换sdWebImage的缓存图片
            [[SDImageCache sharedImageCache] storeImage:newImage forKey:item.image1];
        }];
    }else{
        self.gifImageView.hidden = YES;
        self.ClickBut.hidden = YES;
        [self.pictureImageView sd_setImageWithURL:[NSURL URLWithString:item.image1]];
    }
    
}

@end
